<template>
  <div class="login">
    <div class="form lg:w-4/10 md:w-6/12 login-box" style="padding: 2em 3em">
      <div class="text-center mb-3">
        <h2 class="text-[30px]">
          {{ $t('modules.login.sign_in') }}
        </h2>
      </div>
      <div>
        <div class="text-center">
          <p class="text-secondary mb-5">
            {{ $t('modules.login.sign_in_and_operate_your_terminal') }}
          </p>
        </div>
      </div>
      <el-form
        ref="loginForm"
        class="form"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        auto-complete="on"
        @submit.native.prevent
      >
        <el-form-item
          :label="$t('modules.login.account_email')"
          class="font-bold"
          prop="username"
        >
          <el-input
            ref="username"
            v-model.trim="loginForm.username"
            :placeholder="$t('modules.login.message.enter_your_username')"
            tabindex="1"
            auto-complete="on"
            @blur="getCaptcha"
          />
        </el-form-item>
        <el-form-item
          :label="$t('modules.login.password')"
          class="font-bold"
          prop="password"
        >
          <el-input
            v-model="loginForm.password"
            type="password"
            tabindex="2"
            auto-complete="on"
            :placeholder="$t('modules.login.message.enter_your_password')"
          />
        </el-form-item>
        <el-form-item
          v-if="loginForm.username"
          :label="$t('modules.login.captcha')"
          class="font-bold captcha w-full"
          prop="captcha"
        >
          <el-row :gutter="20" class="w-full">
            <el-col :span="16">
              <el-input
                v-model.trim="loginForm.captcha"
                @keyup.enter.native="handleLogin"
              />
            </el-col>
            <el-col :span="8" class="login-captcha">
              <button>
                <img
                  ref="captcha"
                  style="height: 32px; float: right"
                  alt
                  @click="getCaptcha"
                />
              </button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-checkbox>
            {{ $t('modules.login.i_accept') }}
            <a class="text-blue-400 hover:text-blue-400">{{
              $t('modules.login.terms_and_conditions')
            }}</a>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            native-type="submit"
            type="primary"
            class="w-full"
            :loading="loading"
            @click="handleLogin"
          >
            {{ $t('modules.login.sign_in') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import SignUp from './SignUp.vue'
import { getLoginCode } from '@/api/auth'

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error(this.$t('modules.login.message.enter_password_tip')))
      } else if (value.length > 18) {
        callback(
          new Error(this.$t('modules.login.message.enter_password_max_length'))
        )
      } else {
        callback()
      }
    }
    return {
      loginForm: {},
      signUpStep: 1,
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('modules.login.message.enter_your_username')
          }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        captcha: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('modules.login.message.enter_captcha')
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      captchaLoading: false,
      dialogVisible: false
    }
  },
  components: {
    SignUp
  },
  methods: {
    getCaptcha() {
      getLoginCode(new Date().getTime(), this.loginForm.username).then(
        (res) => {
          this.$refs.captcha.src = res.captcha
        }
      )
    },
    startSignUp() {
      this.signUpStep = 1
      this.dialogVisible = true
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch((e) => {
              this.loading = false
              this.getCaptcha()
              this.$message.error(e.message)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mounted() {
    this.getCaptcha()
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss">
.login {
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: inherit;
  }
  .captcha {
    .el-form-item__label {
      float: none;
    }
  }
}
</style>

<style lang="scss" scoped>
.login {
  padding: 40px;
  min-height: 100%;
  background-image: url('../../assets/images/sign-in-up/sign-in.png');
  display: flex;
  align-items: center !important;
  justify-content: center;
  background-size: cover;
  border-radius: 3px;
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.5);
  .login-box {
    max-width: 500px;
    max-height: 650px;
  }
}

.form {
  background-color: white;
  border-radius: 10px;
}

@media screen and (min-width: 992px) {
  .login {
    display: flex;
    .login-box {
      width: 45%;
      margin-right: 50%;
    }
    background-image: linear-gradient(
        100deg,
        rgba(255, 255, 255, 0.95) 0%,
        rgba(255, 255, 255, 0.95) 50%,
        transparent 40%
      ),
      url('../../assets/images/sign-in-up/sign-in.png');
    /*display: block;*/
  }
  .form {
    background-color: transparent;
  }
}

@media screen and (min-width: 1200px) {
  .login {
    .login-box {
      width: 35%;
      margin-right: 60%;
    }
  }
}
</style>
